package com.dataaccess.dataaccess.support.aibox;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/9/3 15:38
 */
@Component
public class QueryDeviceDetail {
    @Resource
    QueryDevicePictureFile queryDevicePictureFile;
    public  void queryDeviceDetail (JSONObject jsonObject){

        //获取数据
        JSONObject value = jsonObject.getJSONObject("value");
        System.out.println("++++++value+++++:"+value);
        if (value!=null){
            //ProductKey
            String productKey = value.getStr("ProductKey");
            //DeviceName
            String deviceName = value.getStr("DeviceName");
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4FuyYyG4NhCy9gVuuBnV", "FLHWiYBr84G2wnYIS1shfFeOUXKGrE");
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("iot.cn-shanghai.aliyuncs.com");
            request.setVersion("2018-01-20");
            request.setAction("QueryDeviceDetail");
            request.putQueryParameter("RegionId", "cn-shanghai");
            request.putQueryParameter("ProductKey", productKey);
            request.putQueryParameter("DeviceName", deviceName);
            try {
                CommonResponse response = client.getCommonResponse(request);
                jsonObject.put("queryDeviceDetail",JSONUtil.parseObj(response.getData()));
                System.out.println("++++queryDeviceDetail+++++++:"+response.getData());
                //QueryDevicePictureFile.queryDevicePictureFile(jsonObject);
                queryDevicePictureFile.queryDevicePictureFile(jsonObject);
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4FuyYyG4NhCy9gVuuBnV", "FLHWiYBr84G2wnYIS1shfFeOUXKGrE");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("iot.cn-shanghai.aliyuncs.com");
        request.setVersion("2018-01-20");
        request.setAction("QueryDeviceDetail");
        request.putQueryParameter("RegionId", "cn-shanghai");
        request.putQueryParameter("ProductKey", "a10Dg4Ugao5");
        request.putQueryParameter("DeviceName", "8aKKVRQK7iBgpkmlt7fF");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
